package com.aiclient.service;

public class MarkdownService {
    private boolean isDarkTheme = false;
    
    public void setDarkTheme(boolean dark) {
        this.isDarkTheme = dark;
    }
    
    public boolean isDarkTheme() {
        return isDarkTheme;
    }
    
    public String convertToHtml(String markdown) {
        if (markdown == null || markdown.trim().isEmpty()) {
            return "";
        }
        
        // 简单的 Markdown 转换
        String html = markdown
            .replace("&", "&amp;")
            .replace("<", "&lt;")
            .replace(">", "&gt;")
            .replace("\n", "<br>")
            .replaceAll("\\*\\*(.+?)\\*\\*", "<strong>$1</strong>")  // 粗体
            .replaceAll("\\*(.+?)\\*", "<em>$1</em>")  // 斜体
            .replaceAll("`([^`]+)`", "<code>$1</code>")  // 行内代码
            .replaceAll("```([\\s\\S]*?)```", "<pre><code>$1</code></pre>");  // 代码块
        
        return wrapWithStyles(html);
    }
    
    private String wrapWithStyles(String html) {
        String backgroundColor = isDarkTheme ? "#2d2d2d" : "#ffffff";
        String textColor = isDarkTheme ? "#ffffff" : "#000000";
        String codeBackground = isDarkTheme ? "#363636" : "#f6f8fa";
        String borderColor = isDarkTheme ? "#505050" : "#dfe2e5";
        
        return String.format("""
            <!DOCTYPE html>
            <html>
            <head>
                <style>
                    body {
                        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
                        line-height: 1.6;
                        padding: 8px;
                        margin: 0;
                        background-color: %s;
                        color: %s;
                    }
                    pre {
                        background-color: %s;
                        border-radius: 6px;
                        padding: 16px;
                        overflow: auto;
                    }
                    code {
                        font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
                        background-color: %s;
                        border-radius: 3px;
                        padding: 0.2em 0.4em;
                    }
                    pre code {
                        background-color: transparent;
                        padding: 0;
                    }
                    table {
                        border-collapse: collapse;
                        width: 100%%;
                        margin: 16px 0;
                    }
                    th, td {
                        border: 1px solid %s;
                        padding: 6px 13px;
                    }
                    th {
                        background-color: %s;
                    }
                    img {
                        max-width: 100%%;
                    }
                    a {
                        color: #0096c9;
                    }
                    blockquote {
                        border-left: 4px solid %s;
                        margin: 0;
                        padding-left: 16px;
                        color: %s;
                    }
                    hr {
                        border: none;
                        border-top: 1px solid %s;
                        margin: 16px 0;
                    }
                    ul, ol {
                        padding-left: 2em;
                    }
                </style>
            </head>
            <body>
                %s
            </body>
            </html>
            """, 
            backgroundColor, textColor, 
            codeBackground, codeBackground, 
            borderColor, codeBackground,
            borderColor, isDarkTheme ? "#cccccc" : "#666666",
            borderColor, html);
    }
} 